package top.mhtools.pigeon.wechat.service.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.mhtools.pigeon.common.enums.DingMessageTypeEnum;
import top.mhtools.pigeon.common.persistence.WeChatRequest;
import top.mhtools.pigeon.template.service.TemplateService;
import top.mhtools.pigeon.wechat.dto.WeChatMessageReqDTO;

import javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * MD消息发送器
 *
 * @author machen
 * @date 2020/4/24 19:34
 */
@Slf4j
@Component
public class WeChatMessageMdSolver extends AbstractWeChatMessageExecuteStrategy {

    @Resource(name = "dingTemplateServiceImpl")
    private TemplateService templateService;

    private static final Pattern pattern = Pattern.compile("@(.*?)@");

    @Override
    public void execute(WeChatRequest weChatRequest) {
//        List<String> atList = weChatRequest.getAtMobiles();
        WeChatMessageReqDTO.Markdown messageReqDTO = weChatRequest.getMarkdown();
//
//        // 指定消息类型
//        DingTalkClient client = new DefaultDingTalkClient(serverUrl);
//        OapiRobotSendRequest request = new OapiRobotSendRequest();
//        request.setMsgtype(DingMessageTypeEnum.MD.getName());
//
//        String titleStr = "";
//        String markStr = "";
//
//        if (StringUtils.isNotEmpty(weChatRequest.getTemplateId())) {
//            DingTemplateRespDTO template = (DingTemplateRespDTO) templateService.getTemplate(new TemplateReqDTO(weChatRequest.getTemplateId()));
//            String contentTemplateStr = template.getContent();
//
//            Map<String, Object> params = messageReqDTO.getTextMap();
//
//            if (CollectionUtil.isNotEmpty(atList)) {
////                request.setAt(buildAt(atList));
//                String atStr = atList.stream().map(each -> each = "@" + each).collect(Collectors.joining(" "));
//                params.put("at", atStr);
//            }
//
//            Matcher matcher = pattern.matcher(contentTemplateStr);
//            while (matcher.find()) {
//                // 占位符
//                String placeholder = matcher.group();
//                String key = placeholder.substring(1, placeholder.length() - 1);
//                if (params.containsKey(key)) {
//                    String value = params.get(key).toString().trim();
//                    contentTemplateStr = StringUtils.replace(contentTemplateStr, placeholder, value);
//                }
//            }
//
//            markStr = Arrays.stream(StrUtil.split(contentTemplateStr, "&")).collect(Collectors.joining("\n\n"));
//        } else {
//            if (CollectionUtil.isNotEmpty(atList)) {
////                request.setAt(buildAt(atList));
//            }
//            markStr = messageReqDTO.getText();
//        }
        WeChatMessageReqDTO weChatReqDTO = new WeChatMessageReqDTO();
        weChatReqDTO.setMsgtype(DingMessageTypeEnum.MD.getName());
        weChatReqDTO.setMarkdown(messageReqDTO);
        weChatReqDTO.setToken(weChatRequest.getToken());

        sendMessage(weChatReqDTO);
    }

    @Override
    public String supports() {
        return DingMessageTypeEnum.MD.getName();
    }
}
